/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package App1.bookShop.controllers;

import APP1.GUI2.MainFrame_2;
import APP1.bookShop.people.Author;
import APP1.bookShop.book.Book;
import APP1.bookShop.book.EBook;
import APP1.bookShop.book.PaperBook;
import APP1.bookShop.main.BookShop;
import APP1.bookShop.people.Publisher;
import APP1.bookShop.stock.Stock;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Asus
 */
public class BookController {

    private BookShop bookShop;

    public BookController() {
    }

    public BookController(BookShop bookShop) {
        this.bookShop = bookShop;
    }

    public DefaultTableModel getBookTitleListTableModel() {
        DefaultTableModel dtm = new DefaultTableModel(new Object[]{"Books Title"}, 0);
        Object[] row;
        for (Book book : bookShop.getBooks()) {

            row = new Object[]{book};
            dtm.addRow(row);
        }
        return dtm;
    }

    public void handleDetailBook(int bookIndex) {
        if (bookIndex >= 0) {
            Book b = bookShop.getBooks().get(bookIndex);
            //update GUI
            if (b != null) {
                MainFrame_2 GUI = bookShop.getGUI();
                GUI.getBookTitleTextField().setText(b.getTitle());
                GUI.getPublishingDateDatePicker().setDate(b.getPublishingDate());
                GUI.getISBNTextField().setText(b.getISBN());
               GUI.getBookPublisherComboBox().setSelectedItem(b.getPublisher());
                if (b instanceof EBook) {
                    GUI.getEbookRadioButton1().setSelected(true);

                } else {
                    GUI.getPaperRadioButton2().setSelected(true);

                }
            }
        }

    }



    public boolean checkInputs(String title, Date publishingDate, String ISBN, Boolean type,Publisher pub,ListModel authors) {
        return authors!=null && authors.getSize()>0 && pub!=null && type != null && title != null && publishingDate != null && ISBN != null && title.length() > 0 && ISBN.length() > 0;
    }

    public ListModel getBookTitleListModel() {
        DefaultListModel lm = new DefaultListModel();

        for (Book book : bookShop.getBooks()) {
            lm.addElement(book.getTitle()+"#"+bookShop.getBookStocks().get(book).getCopies());

        }
        if (lm.size() <= 0) {
            lm.addElement("No Books");
        }

        return lm;
    }

    public DefaultListModel getAuthorsofBookListModel(Book a) {
        DefaultListModel lm = new DefaultListModel();

        for (Author author : a.getAuthors()) {
            // lm.addElement(author.getFirstName()+" "+author.getLastName());
            lm.addElement(author);
        }
        if (lm.size() <= 0) {

            lm.addElement("No Authors");
        }
        return lm;
    }

    public String addBook(String title, Date publishingDate, String ISBN, boolean type,Publisher pub,ListModel authors) {
        if (!checkInputs(title, publishingDate, ISBN, type,pub,authors)) {
            return "Invalid Input";
        } else {

            Book b;
            
            if (type) {
                b = new PaperBook();
            } else {
                b = new EBook();
            }

            b.setTitle(title);
            b.setPublishingDate(publishingDate);
            b.setPublisher(pub);
              b.setIsbn(ISBN);

           DefaultListModel authorsDLM = (DefaultListModel) authors;
            for (int i = 0; i < authors.getSize(); i++) {
                Author auth =(Author) authors.getElementAt(i);
                b.addAuthor(auth);
                
            }
            bookShop.getBooks().add(b);

            if(!bookShop.getBookStocks().containsKey(b)){
                bookShop.getBookStocks().put(b, new Stock(0));
            }
            
            bookShop.getGUI().getBookList().setModel(getBookTitleListModel());

        }
        return "Saved";
    }

    public void deleteBook(Book book) {
        for (Author author : bookShop.getAuthors()) {
            author.getBookList().remove(book);

        }

        for (Publisher pub : bookShop.getPublishers()) {
            pub.getBook().remove(book);
        }
        
        bookShop.getBooks().remove(book);
        
        //updateGUI
       
        
    }

}
